package com.cyf.db.mapper;

import com.cyf.db.bean.BookingInfo;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

/**
 * 车位预约
 * Created by cage on 2017/4/7.
 */

public interface BookingInfoMapper {

    @Insert("insert into booking_info(appUserId, parkingId, createdTime, bookingStatus, sendToParking, feeType, fee, parkingLockId, endTime) " +
            "values(#{bookingInfo.appUserId}, #{bookingInfo.parkingId}, #{bookingInfo.createdTime}, #{bookingInfo.bookingStatus}, 0, " +
            "#{bookingInfo.feeType}, #{bookingInfo.fee}, #{bookingInfo.parkingLockId}, #{bookingInfo.endTime})")
    @SelectKey(statement = "select LAST_INSERT_ID()", keyProperty = "bookingInfo.id", before = false, resultType = int.class)
    void insertBookingInfo(@Param("bookingInfo") BookingInfo bookingInfo);

    // 取消预约
    @Update("update booking_info set bookingStatus = #{bookingStatus}, endTime = #{endTime} where id = #{id}")
    void updateStatusAndEndTimeById(@Param("id") Integer id, @Param("bookingStatus") Integer bookingStatus, @Param("endTime") Date endTime);

    @Select("select * from booking_info where id = #{id}")
    BookingInfo getById(@Param("id") Integer id);

    // 更新订单中的地锁id
    @Update("update booking_info set parkingLockId = #{parkingLockId}, bookingStatus = #{bookingStatus} where id = #{id}")
    void updateParkingLockIdAndStatusById(@Param("parkingLockId") Integer parkingLockId, @Param("bookingStatus") Integer bookingStatus, @Param("id") Integer id);

    // 车场已经预约车位数
    @Select("select count(id) from booking_info where parkingId = #{parkingId} and (bookingStatus = 1 or bookingStatus = 2)")
    Integer getCountByParkingId(@Param("parkingId") Integer parkingId);

    // 预约信息已经发送到车场，但是数据更新了，将sendToParking=2
    @Update("update booking_info set sendToParking = #{sendToParking} where id = #{id}")
    void updateSendById(@Param("id") Integer id, @Param("sendToParking") Integer sendToParking);

    // 查询完成的条数
    @Select("select count(id) from booking_info where bookingStatus = 3 and appUserId = #{appUserId}")
    Integer getFinishedCountByAppUserId(@Param("appUserId") Integer appUserId);

    // 查询失效的条数
    @Select("select count(id) from booking_info where bookingStatus = 4 and appUserId = #{appUserId}")
    Integer getFailureCountByAppUserId(@Param("appUserId") Integer appUserId);

    // 查询失效/完成的条数
    @Select("select count(id) from booking_info where (bookingStatus = 3 or bookingStatus = 4) and appUserId = #{appUserId}")
    Integer getHistoryCountByAppUserId(@Param("appUserId") Integer appUserId);

    // 获取未降锁的订单（bookingStatus = 1）
    @Select("select b.*, p.name parkingName from booking_info b, parking p " +
            "where b.parkingId = p.id " +
            "and b.bookingStatus = 1 and b.appUserId = #{appUserId} " +
            "order by b.createdTime desc")
    List<BookingInfo> getBooked(@Param("appUserId") Integer appUserId);

    // 获取未升锁的订单（bookingStatus = 2）
    @Select("select b.*, p.name parkingName, l.name parkingLockName from booking_info b, parking p, parkinglock_info l " +
            "where b.parkingId = p.id and b.parkingLockId = l.id " +
            "and b.bookingStatus = 2 and b.appUserId = #{appUserId} " +
            "order by b.createdTime desc")
    List<BookingInfo> getProcessing(@Param("appUserId") Integer appUserId);

    // 获取完成的订单（bookingStatus = 3）
    @Select("select b.*, p.name parkingName, l.name parkingLockName from booking_info b, parking p, parkinglock_info l " +
            "where b.parkingId = p.id and b.parkingLockId = l.id " +
            "and b.bookingStatus = 3 and b.appUserId = #{appUserId} " +
            "order by b.endTime desc limit #{start}, #{end}")
    List<BookingInfo> getFinished(@Param("appUserId") Integer appUserId, @Param("start") int start, @Param("end") int end);

    // 获取失效的订单（bookingStatus = 4）（取消预约和超时的）
    @Select("select b.*, p.name parkingName from booking_info b, parking p " +
            "where b.parkingId = p.id " +
            "and b.appUserId = #{appUserId} and b.bookingStatus = 4 order by b.endTime desc limit #{start}, #{end}")
    List<BookingInfo> getFailure(@Param("appUserId") Integer appUserId, @Param("start") int start, @Param("end") int end);

    // 获取完成/失效的订单
    @Select("select b.*, p.name parkingName, l.name parkingLockName from booking_info b " +
            "LEFT JOIN parking p ON b.parkingId = p.id " +
            "LEFT JOIN parkinglock_info l on b.parkingLockId = l.id " +
            "where (b.bookingStatus = 3 or b.bookingStatus = 4) and b.appUserId = #{appUserId} order by b.endTime desc limit #{start}, #{end}")
    List<BookingInfo> getHistory(@Param("appUserId") Integer appUserId, @Param("start") int start, @Param("end") int end);

    // 删除旧的订单（每天凌晨1点删除5天前的失效/完成的）
    @Delete("delete from booking_info where endTime < #{date}")
    void deleteTimeout(@Param("date") Date date);

    // 获取预约超时但有效的订单
    @Select("select * from booking_info where endTime < #{now} and bookingStatus = 1")
    List<BookingInfo> getTimeOut(Date now);
}
